package com.zx.system.block.controller.system;

import com.zx.core.enums.StatusEnum;
import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.system.block.model.SysTenant;
import com.zx.system.block.param.TenantParam;
import com.zx.system.block.service.ISysTenantService;
import com.zx.system.block.vo.SysTenantVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.StatusUpdate;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * description: 租户管理控制器 <br>
 * create: 2025-11-23
 * </p>
 *
 * @author zhou xun
 */
@RestController
@RequestMapping("/tenant")
@RequiredArgsConstructor
public class SysTenantController {
    private final ISysTenantService iSysTenantService;

    @PreAuthorize("hasAuthority('system:tenant:list')")
    @PostMapping("/list")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "租户参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<SysTenantVO> list(@RequestBody TenantParam tenantParam) {
        return iSysTenantService.getTenantVOListByPage(tenantParam);
    }

    /**
     * 查询租户树
     *
     * @param tenantId 租户Id
     * @return {@link ListWrapper}
     * @author zhou xun
     * @since 2025-11-23
     */
    @PreAuthorize("hasAuthority('system:tenant:list')")
    @GetMapping("/tree/{tenantId}")
    public ListWrapper<SysTenantVO> getTenantTree(@PathVariable("tenantId") String tenantId) {
        ListWrapper<SysTenant> tenantListWrapper = iSysTenantService.selectTenantTree(tenantId);
        ListWrapper<SysTenantVO> result = new ListWrapper<>();

        if (tenantListWrapper.verifySuccess()) {
            // 转换为VO对象
            List<SysTenantVO> voList = tenantListWrapper.getData().stream()
                    .map(sysTenant -> {
                        SysTenantVO sysTenantVO = SysTenantVO.fromSysTenant(sysTenant);
                        StatusEnum statusEnum = StatusEnum.getEnumByCode(sysTenant.getStatus());
                        if (statusEnum != null) {
                            sysTenantVO.setStatusDesc(statusEnum.getDesc());
                        }
                        return sysTenantVO;
                    })
                    .collect(Collectors.toList());
            result.setData(voList);
        } else {
            result.fail(tenantListWrapper.getMsg());
        }

        return result;
    }

    /**
     * 新增租户
     *
     * @param tenantParam 租户信息
     * @return {@link ResponseWrapper}
     * @author zhou xun
     * @since 2025-11-23
     */
    @PreAuthorize("hasAuthority('system:tenant:add') and hasRole('admin')")
    @OperateLog(value = "新增租户")
    @PostMapping("/addTenant")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "租户参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addTenant(@RequestBody TenantParam tenantParam) {
        return iSysTenantService.addTenant(tenantParam);
    }

    /**
     * 编辑租户
     *
     * @param tenantParam 租户信息
     * @return {@link ResponseWrapper}
     * @author zhou xun
     * @since 2025-11-23
     */
    @PreAuthorize("hasAuthority('system:tenant:edit') and hasRole('admin')")
    @OperateLog(value = "编辑租户")
    @PutMapping("/editTenant")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "租户参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editTenant(@RequestBody TenantParam tenantParam) {
        return iSysTenantService.editTenant(tenantParam);
    }

    /**
     * 修改租户状态
     *
     * @param tenantParam 租户参数
     * @return {@link ResponseWrapper}
     * @author zhou xun
     * @since 2025-11-23
     */
    @PreAuthorize("hasAuthority('system:tenant:edit') and hasRole('admin')")
    @OperateLog(value = "修改租户状态")
    @PutMapping("/editTenantStatus")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "租户参数", rules = {ValidateRulesEnum.OBJECT}, groups = {StatusUpdate.class})
    )
    public ResponseWrapper editTenantStatus(@RequestBody TenantParam tenantParam) {
        return iSysTenantService.editTenantStatus(tenantParam.getId(), tenantParam.getStatus());
    }

    /**
     * 删除租户
     *
     * @param tenantId 租户Id
     * @return {@link ResponseWrapper}
     * @author zhou xun
     * @since 2025-11-23
     */
    @PreAuthorize("hasAuthority('system:tenant:remove') and hasRole('admin')")
    @OperateLog(value = "删除租户")
    @DeleteMapping("/deleteTenant/{tenantId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "租户Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteTenant(@PathVariable("tenantId") String tenantId) {
        return iSysTenantService.deleteTenant(tenantId);
    }
}